package cn.jinjimi.base.util;

import android.graphics.Paint;
import android.widget.TextView;

import java.util.Calendar;

/**
 * Created by Jinsen on 2015/4/17.
 */
public class CommonUtil {

    /**
     * @param textview
     * @param lineHeight
     */
    public static void setTextLineHight(TextView textview, float lineHeight){
        Paint paint = new Paint();
        paint.setTextSize(textview.getTextSize());
        Paint.FontMetrics fm = paint.getFontMetrics();

        float fFontHeight = (float)Math.ceil(fm.descent - fm.ascent);

        float fAddValue = 0;
        if(fFontHeight < lineHeight){
            fAddValue = lineHeight - fFontHeight;
        }
        textview.setLineSpacing(fAddValue,1);
    }

    /**
     * 计算生日倒计时
     * @param birthday
     * @return
     */
    public static int getBirthDayCountDown(long birthday){
        try {
            Calendar cToday = Calendar.getInstance(); // 存今天
            Calendar cBirth = Calendar.getInstance(); // 存生日
            cBirth.setTimeInMillis(birthday);
            cBirth.set(Calendar.YEAR, cToday.get(Calendar.YEAR)); // 修改为本年
            int days;
            if (cBirth.get(Calendar.DAY_OF_YEAR) < cToday.get(Calendar.DAY_OF_YEAR)) {
                // 生日已经过了，要算明年的了
                days = cToday.getActualMaximum(Calendar.DAY_OF_YEAR) - cToday.get(Calendar.DAY_OF_YEAR);
                days += cBirth.get(Calendar.DAY_OF_YEAR);
            } else {
                // 生日还没过
                days = cBirth.get(Calendar.DAY_OF_YEAR) - cToday.get(Calendar.DAY_OF_YEAR);
            }
            return days;
        }catch (Exception e){
            return -1;
        }
    }

}
